package com.htfyun.roobofactory.htfyunUtils;


import android.hardware.roobo.INVRomDataService;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.roobo.common.utils.Utils;

//import android.os.ServiceManager;

/**
 * Created by tony on 17-3-25.
 */
public class NvDataTool {
    private static String TAG = "NvDataTool";

    private static NvDataTool sInstance;
    private INVRomDataService mService = null;

    public final static String PC_TOOL_FT_CHECKED = "CHECKED";
    public final static String PC_TOOL_FT_UNCHECKED = "UNCHECKED";

    public enum NvDataKeyIntegerAttr {
        ft_checked("str_key_pctool_ft_checked"),//是否全部测试标志
        pcba_result("str_key_pcba_test_result"),//pcba 测试结果, 由pcba那边保存的
        cx20921_flag("int_key_cx20921_download_flag"),//cx20921 下载成功标志, 由pcba那边保存的
        ps_noise("int_key_proximity_sensor_noise"),//近距离传感器 底噪 校准数据.
        ps_difference("int_key_proximity_sensor_difference"),// 近距离传感器 差分 校准数据.
        ;
        private final String key;
        NvDataKeyIntegerAttr(String key) {
    		this.key = key;
    	}
        public String getKey() {
    		return key;
    	}
    }


    public static final int VNROM_DATA_UNSET_DEFAULT_VALUE = 0XF0F1F2FD;


    private NvDataTool() {
//        IBinder binder = ServiceManager.getService("NVRomDataService");
        IBinder binder = Utils.getSystemServiceReflect("NVRomDataService");
        if (binder == null) {
            Log.e(TAG, "could not retrieve NVRomDataService service");
            throw new UnsupportedOperationException();
        }
        Log.e(TAG, "NvDataTool binder = " + binder);
        mService = INVRomDataService.Stub.asInterface(binder);
    }

    public static synchronized NvDataTool getInstance() {
        if (sInstance == null) {
            sInstance = new NvDataTool();
        }
        return sInstance;
    }

    public String getAllIntegerInfoFromNVRomData(int startOffset, int count) {
        try {
            return mService.getAllIntegerInfo(startOffset, count);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return "";
    }

    public int getCountOfAllIntegerInfo() {

        try {
            return mService.getCountOfAllIntegerInfo();
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return 0;
    }

    public String getAllStringInfoFromNVRomData(int startOffset, int count) {
        try {
            return mService.getAllStringInfo(startOffset, count);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return "";
    }

    public int getCountOfAllStringInfo() {

        try {
            return mService.getCountOfAllStringInfo();
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return 0;
    }

    public boolean removeIntegerFromNVRomData(String key) {
        try {
            return mService.removeIntegerNVRomData(key);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return false;
    }

    public boolean removeStringFromNVRomData(String key) {
        try {
            return mService.removeStringNVRomData(key);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return false;
    }

    public int getNVRomData(String key, int defValue) {
        if (TextUtils.isEmpty(key)) {
            return defValue;
        }

        try {
            return mService.getNVRomData(key, defValue);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return defValue;
    }
    public boolean setNVRomData(String key, int value) {
        if (TextUtils.isEmpty(key)) {
            return false;
        }
        try {
             return mService.setNVRomData(key, value);
         } catch (RemoteException e) {
             Log.e(TAG, "RemoteException in setNVRomData", e);
         }
         return false;

    }

    /**
     * check weather the data at NVRom is set by user.
     * @param attr
     * @return
     */
    public boolean isNVRomDataSet(NvDataKeyIntegerAttr attr) {
        int value = getNVRomData(attr.getKey(), VNROM_DATA_UNSET_DEFAULT_VALUE);
        return value != VNROM_DATA_UNSET_DEFAULT_VALUE;
    }

    public boolean saveNVRomString(String key, String str) {
        if (TextUtils.isEmpty(key)) {
            return false;
        }

        try {
            return mService.saveNVRomString(key, str);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return false;
    }

    public String readNVRomString(String key) {
        if (TextUtils.isEmpty(key)) {
            return "";
        }
        try {
            return mService.readNVRomString(key);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setNVRomData", e);
        }
        return "";
    }


    ///////////////////////////////////////////
    public boolean isFactoryTestChecked() {
        String checked = readNVRomString(NvDataKeyIntegerAttr.ft_checked.getKey());
        if (TextUtils.isEmpty(checked)) {
            return false;
        }

        return checked.equals(PC_TOOL_FT_CHECKED);
    }

    public void setFactoryCheck(boolean checked) {
        saveNVRomString(NvDataKeyIntegerAttr.ft_checked.getKey(), checked ? PC_TOOL_FT_CHECKED : PC_TOOL_FT_UNCHECKED);
    }

    public String getPCBAResult() {
        return readNVRomString(NvDataKeyIntegerAttr.pcba_result.getKey());
    }

    public int getCX20921DownloadFlag() {
        return getNVRomData(NvDataKeyIntegerAttr.cx20921_flag.getKey(), 0);
    }

    public int getProximitySensorNoise() {
        return getNVRomData(NvDataKeyIntegerAttr.ps_noise.getKey(), 0);
    }
    public boolean setProximitySensorNoise(int value) {
        return setNVRomData(NvDataKeyIntegerAttr.ps_noise.getKey(), value);
    }
    public boolean isProximitySensorNoiseSet() {
        return isNVRomDataSet(NvDataKeyIntegerAttr.ps_noise);
    }

    public int getProximitySensorDifference() {
        return getNVRomData(NvDataKeyIntegerAttr.ps_difference.getKey(), 0);
    }
    public boolean setProximitySensorDifference(int value) {
        return setNVRomData(NvDataKeyIntegerAttr.ps_difference.getKey(), value);
    }
    public boolean isProximitySensorDifferenceSet() {
        return isNVRomDataSet(NvDataKeyIntegerAttr.ps_difference);
    }

}
